package com.apanal.qlife.advice.controller;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.apanal.qlife.advice.model.Advice;
import com.apanal.qlife.advice.service.SysAnnounceService;
import com.apanal.qlife.advice.service.SysInformService;
import com.apanal.qlife.common.annotation.CurrentUser;
import com.apanal.qlife.common.annotation.LogType;
import com.apanal.qlife.common.annotation.SystemLog;
import com.apanal.qlife.common.constants.Constants;
import com.apanal.qlife.common.page.Page;
import com.apanal.qlife.common.page.PageReturn;
import com.apanal.qlife.org.model.Organization;
import com.apanal.qlife.org.service.OrganizationService;
import com.apanal.qlife.sys.model.Role;
import com.apanal.qlife.sys.model.User;
import com.apanal.qlife.sys.service.RoleService;
import com.apanal.qlife.sys.service.UserService;

@Controller
@RequestMapping("/announce")
public class SysAnnounceController {

	@Autowired
	private SysAnnounceService sysAnnounceservice;

	@Autowired
	private OrganizationService orgService;

	@Autowired
	private RoleService roleService;

	@Autowired
	private SysInformService sysInformService;

	@Autowired
	private UserService userService;

	/**
	 * 查询所有已发布系统公告
	 * 
	 * @param model
	 * @return
	 */
	@SystemLog(module = "系统公告管理", operation = "分页查询系统已发布公告", type = LogType.SELECT)
	@RequiresPermissions("advice:announce:view")
	@RequestMapping(value = "list")
	public String list(Model model, Advice advice, Page page) {
		PageReturn<Advice> pr = sysAnnounceservice.findAdviceBycond(advice,
				page);
		model.addAttribute("result", pr);
		model.addAttribute("advice", advice);
		System.out.println("=============");
		return "advice/sysAnnounceList";
	}

	/**
	 * 查询所有未发布系统公告
	 * 
	 * @param model
	 * @return
	 */
	@SystemLog(module = "系统公告管理", operation = "分页查询系统未发布公告", type = LogType.SELECT)
	@RequiresPermissions("advice:announce:view")
	@RequestMapping(value = "listNo")
	public String listNo(Model model, Advice advice, Page page) {
		PageReturn<Advice> pr = sysAnnounceservice.findAdviceBycondNo(advice,
				page);
		model.addAttribute("result", pr);
		model.addAttribute("advice", advice);
		return "advice/sysAnnounceListNo";
	}

	/**
	 * 新增系统公告
	 * 
	 * @param model
	 * @return
	 */
	@RequiresPermissions("advice:announce:create")
	@RequestMapping(value = "annouceAdd", method = RequestMethod.GET)
	public String annouceAdd(@CurrentUser User user, Advice advice, Model model) {

		// 如果当前登录的是总后台管理员，那么查找所有平台
		Role role = roleService.findRoleNameByid(user.getUseId());
		String flg = "1";
		if (role != null) {
			List<Organization> platList = orgService.findAllPlat();
			model.addAttribute("platList", platList);
			flg = "2";
		} else {
			// 如果当前登录人不是总后台管理员 ，那么根据当前登录人找到对应的平台
			Organization org = orgService.findPaltByUser(user.getUseId());
			model.addAttribute("org", org);
		}
		model.addAttribute("flg", flg);
		return "advice/announceAdd";
	}

	/**
	 * 新增系统公告保存
	 * 
	 * @param model
	 * @return
	 */
	@SystemLog(module = "系统公告管理", operation = "新增系统公告", type = LogType.CREATE)
	@RequiresPermissions("advice:announce:create")
	@ResponseBody
	@RequestMapping(value = "saveAnnounce", method = RequestMethod.POST)
	public String saveAnnounce(@CurrentUser User user, Model model,
			Advice advice) throws ParseException {

		sysAnnounceservice.insert(advice, user);
		return Constants.DEFAULT_MSG_SUCCESS;
	}

	/**
	 * 查看公告详情
	 * 
	 * @param model
	 * @return
	 */
	@RequiresPermissions("advice:announce:view")
	@RequestMapping(value = "/{advId}/listDetails")
	public String listDetails(@CurrentUser User user, Model model, Advice advice) {
		Advice ac = sysAnnounceservice.findAppAnnouceDtail(advice);
		model.addAttribute("advice", ac);
		return "advice/listDetails";
	}

	/**
	 * 编辑公告
	 * 
	 * @param model
	 * @return
	 */

	@RequiresPermissions("advice:announce:update")
	@RequestMapping(value = "/{advId}/annouceEdit")
	public String annouceEdit(@CurrentUser User user, Model model, Advice advice) {
		Role role = roleService.findRoleNameByid(user.getUseId());
		Advice ac = sysAnnounceservice.findAppAnnouceDtail(advice);
		String flg = "1";
		if (role != null) { // 超级管理员
			List<Organization> platList = orgService.findAllPlat();
			model.addAttribute("platList", platList);
			flg = "2";
		}
		if (ac.getSetTypeDetail() == 12) {
			Organization org = ac.getOrgList().get(0);
			model.addAttribute("orgName", org.getOrgName());
			model.addAttribute("orgId", org.getOrgId());
		}
		model.addAttribute("advice", ac);
		model.addAttribute("flg", flg);
		return "advice/annouceEdit";
	}

	/**
	 * 编辑公告保存
	 * 
	 * @param model
	 * @return
	 */
	@SystemLog(module = "系统公告管理", operation = "修改系统公告", type = LogType.UPDATE)
	@RequiresPermissions("advice:announce:update")
	@ResponseBody
	@RequestMapping(value = "saveAnnounceEdit", method = RequestMethod.POST)
	public String saveAnnounceEdit(@CurrentUser User user, Model model,
			Advice advice) throws ParseException {
		// 总后台管理员
		Advice ac = sysAnnounceservice.findDetailById(advice.getAdvId());
		sysAnnounceservice.updateAnnounce(advice, user, ac);
		return Constants.DEFAULT_MSG_SUCCESS;
	}

	/**
	 * 系统公告发送
	 * 
	 * @param model
	 * @return
	 */
	@SystemLog(module = "系统公告管理", operation = "发送系统公告", type = LogType.UPDATE)
	@RequiresPermissions("advice:announce:send")
	@RequestMapping(value = "/{advId}/SendAnnounce")
	public String SendAnnounce(@CurrentUser User user, Model model,
			Advice advice) throws ParseException {

		// 更新公告表
		Advice ac = sysInformService.findInformDetailById(advice.getAdvId());
		ac.setStartDate(new Date());
		ac.setLastUpdateBy(user.getUseId());
		sysAnnounceservice.modifyAnnounce(ac);
		return "redirect:/announce/list";
	}

	/**
	 * 系统公告关闭
	 * 
	 * @param model
	 * @return
	 */
	@SystemLog(module = "系统公告管理", operation = "关闭系统公告", type = LogType.DELETE)
	@RequiresPermissions("advice:announce:delete")
	@ResponseBody
	@RequestMapping(value = "/{advId}/CloseAnnounce")
	public String CloseAnnounce(@CurrentUser User user, Model model,
			Advice advice) throws ParseException {

		// 更新公告表
		// Advice ac = sysInformService.findInformDetailById(advice.getAdvId());
		// ac.setStatus(Constants.IS_VALID_FALSE);
		// ac.setLastUpdateBy(user.getUseId());
		sysAnnounceservice.deleteAnnounce(advice);

		return Constants.DEFAULT_MSG_SUCCESS;
	}

	/**
	 * 首页系统公告列表接口
	 * 
	 * @param model
	 * @return
	 */

	@RequestMapping(value = "indexAnnouceList")
	public String indexAnnouceList(Model model, Advice advice, Page page) {
		User user = userService.getCurrentUser();
		PageReturn<Advice> pr = sysAnnounceservice.findAonnce(user.getOrgId(),
				page, advice);
		model.addAttribute("result", pr);
		model.addAttribute("advice", advice);
		System.out.println("=============");
		return "advice/sysIndexAnnounceList";
	}

	/**
	 * 首页系统公告列表查询接口
	 * 
	 * @param model
	 * @return
	 */

	@RequestMapping(value = "/{advId}/indexListDetails")
	public String indexListDetails(@CurrentUser User user, Model model,
			Advice advice) {
		Advice ac = sysAnnounceservice.findSysAnnounceDtail(advice.getAdvId());
		model.addAttribute("advice", ac);
		return "advice/listIndexDetails";
	}

}
